//
//  PointController.m
//  HB
//
//  Created by 陶秋安 on 16/5/13.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "PointController.h"
#import "PointDetailController.h"

@implementation PointController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self myPoint];
 
}

- (void)viewDidAppear:(BOOL)animated
{
    
}
- (IBAction)btnAmountRequest:(id)sender {
    
    [UIView showAlertView:@"提示" andMessage:@"确定要提现吗？" andHost:self sel:^(UIAlertAction *action) {
        [self amountRequest];
    }];
}
 
-(void)setNavigationLeft:(NSString *)imageName sel:(SEL)sel
{


}

-(void) amountRequest
{
    operation=[[PointOperation alloc] initWithDelegate:self];
    //开始保存数据
    
    NSString *url= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"AmountRequest"];
    
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    NSString *md5Str= [NSString stringWithFormat: @"djID=%@&key=tqa",djID];
    
    NSString *sign = [Common createMD5:md5Str];
    
    NSString *body=[NSString stringWithFormat:@"{\"djID\":\"%@\",\"sign\":\"%@\",\"clicentid\":\"1\"}",djID,sign];
    
    NSDictionary *dictInfo = @{@"url":url,@"body":body};
    
    [operation setOpInfo:dictInfo];
    
    [operation post];

}

-(void)opSuccessForPost:(id)data
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        
        [UIView showAlertView:@"提示" andMessage:@"提现申请已经提交，我们会在3个工作日内为你发放" andHost:self];
    }
    else if([v isEqualToString:@"4000001"])
    {
        [UIView showAlertView:@"提示" andMessage:@"您还有未处理的提现请求。" andHost:self];
    }

}

- (void)setNavigationRight:(NSString *)imageName sel:(SEL)sel
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(0, 0, 66, 22);
    
    [btn setTitle:@"积分明细" forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    
    [btn addTarget:self action:@selector(pointDetail) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem = item;

}

- (void)setNavigationTitleImage:(NSString *)imageName
{
    self.navigationItem.title = @"我的金库";
}

-(void)pointDetail
{
    PointDetailController *pointDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"PointDetail"];
    
    [self.navigationController pushViewController:pointDetail animated:YES];
}

-(void)myPoint
{
    operation= [[PointOperation alloc] initWithDelegate:self];
    
    NSString *djID= [Common getUserDefaults:@"djID"];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Mine" andFuncName:@"DJPoint"];
    
    NSString *finallURL= [NSString stringWithFormat:@"%@?djID=%@",URL,djID];
    
    NSDictionary * dictInfo = @{@"url":finallURL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

-(void)opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    if(flag==1)
    {
        operation= nil;
 
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *data = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        if ([[data objectForKey:@"result"] isEqualToString:@"1"])
        {
            dispatch_async( dispatch_get_main_queue(), ^(void)
                           {
                               self.lblPoint.text= [data objectForKey:@"point"];
                           });
        }

    }
}

@end
